﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace CIMCService
{
    /// <summary>
    /// SqlDatabaseController 的摘要说明
    /// </summary>
    public class SqlDatabaseController
    {
        private SqlConnection sqlcon;

		private SqlTransaction transaction;

         public SqlDatabaseController()
        {
            //
            // TODO: 在此处添加构造函数逻辑
             this.sqlcon = ConnectionFactory.GetSqlConnection();
			this.transaction = null;
            //
        }

		public void OpenConnection() 
		{
			if (sqlcon.State == ConnectionState.Closed) 
			{
				//sqlcon.ConnectionTimeout = 300;
				sqlcon.Open();
			}
		}

		
		public void CloseConnection() 
		{
			if (sqlcon.State == ConnectionState.Open) 
			{
				sqlcon.Close();
			}
		}


		public int ExecuteNonQuery(SqlCommand command) 
		{
			command.Connection = this.sqlcon;
            command.CommandTimeout = 120000;
			if (this.transaction != null) 
			{
				command.Transaction = this.transaction;
			}
			return command.ExecuteNonQuery();
		}


		public Int32 ExecuteScalar(SqlCommand command) 
		{
			command.Connection = this.sqlcon;
			if (this.transaction != null) 
			{
				command.Transaction = this.transaction;
			}
			return Convert.ToInt32(command.ExecuteScalar());
		}

		public DataSet ExecuteQuery(SqlCommand command) 
		{
            			
			command.Connection = this.sqlcon;
			//command.CommandTimeout = 1000;
			SqlDataAdapter adapter = new SqlDataAdapter();	
			adapter.SelectCommand = command;
			DataSet dset = new DataSet();
			try
			{
				adapter.Fill(dset,"temp");
				adapter.Dispose();
			
			}
			catch (System.Exception err)
			{
				string s = err.Message;
				throw;
			}
			return dset;
		}

        public SqlDataAdapter ExecuteadapaterQuery(SqlCommand command)
        {
            command.Connection = this.sqlcon;
            //command.CommandTimeout = 1000
            SqlDataAdapter lsadapter = new SqlDataAdapter();
            try
            {
                lsadapter.SelectCommand = command;
            }
            catch (System.Exception)
            {
                throw;
            }
            return lsadapter;
        }

        public void updatedatabaser(SqlCommand command, DataSet dtst)
        {
            command.Connection = this.sqlcon;
            if (this.transaction != null)
            {
                command.Transaction = this.transaction;
            }
            SqlDataAdapter lsadapter = new SqlDataAdapter();
            lsadapter.SelectCommand = command;
            try
            {
                SqlCommandBuilder SCB = new SqlCommandBuilder(lsadapter);
                lsadapter.Update(dtst.Tables[0]);
            }
            catch (System.Exception)
            {
                throw;
            }
        }

		public void BeginTransaction() 
		{
			this.transaction = sqlcon.BeginTransaction();
			
		}

		public void Commit() 
		{
			this.transaction.Commit();
			this.transaction = null;
		}

		public void Rollback() 
		{
			this.transaction.Rollback();
			this.transaction = null;
		}

       
    }
}
